<?php
/**
 * create to 2021/6/24 17:52
 */

namespace app\mainadmin\controller;


use app\AdminController;
use app\mainadmin\model\TaskingModel;
use think\facade\Session;

/**
 * 任务配置
 * Class Index
 * @package app\store\controller
 */
class Tasking extends AdminController
{

    /*------------------------------------------------------ */
    //-- 优先执行
    /*------------------------------------------------------ */
    public function initialize(){
        parent::initialize();
        $this->Model = new TaskingModel();
    }

    /*------------------------------------------------------ */
    //-- 主页
    /*------------------------------------------------------ */
    public function index() {
        $this->getList(true);
        return $this->fetch();
    }

    /*------------------------------------------------------ */
    //-- 获取列表
    //-- $runData boolean 是否返回模板
    /*------------------------------------------------------ */
    public function getList($runData = false) {
        $search['report_type'] = input('report_type',0,'intval');
        $search['keyword'] = input('keyword','','trim');
        $where = [];
//        if ($search['status'] >= 0){
//            $where[] = ['status','=',$search['status']];
//        }

        if (empty($page_size)){
            $page_size = input("page_size/d",0);
            $session_page_size = Session::get('page_size') * 1;
            if ($page_size <= 1 ){
                if ($session_page_size <= 1) $session_page_size = 20;
                $page_size = $session_page_size;
            }
            elseif ($page_size != $session_page_size) Session::set('page_size',$page_size);
        }
        $this->sqlOrder = 'sort desc';
        $data = $this->getPageList($this->Model,$where);
        foreach ($data['list'] as &$v) {
            if ($v['createtime']){
                $v['create_time'] = date("Y-m-d H:i:s",$v['createtime']);
            }
        }
        $this->assign("data", $data);
        $this->assign("status", $this->Model->status);
        $this->assign("type", $this->Model->type);
        if ($runData == false){
            $data['content']= $this->fetch('index')->getContent();
            unset($data['list']);
            return $this->success('','',$data);
        }
        return true;
    }

    /*------------------------------------------------------ */
    //-- 上下架
    /*------------------------------------------------------ */
    public function check() {
        $id = input('id',0,'intval');
        $status = input('status',0,'intval');
        if ($id < 1){
            return $this->error('传参错误.');
        }
        $row = $this->Model->where('id',$id)->find();
        if (empty($row)){
            return $this->error('没有找到相关记录.');
        }
        $save_data = ['status'=>$status];
        $res = $this->Model->where('id',$id)->update($save_data);
        if ($res < 1){
            return $this->error('更新审核结果失败.');
        }
        return $this->success('操作成功.');
    }
}